import {
  queryGetCreateTableDDLReq,
  queryGetDatabasesReq,
  queryGetTagetTableFieldsReq,
  queryGetTargetTablesReq,
  queryGetTaskByIdReq,
  queryGetWsdlPortOperationMapReq,
  queryPreviewReq,
  querySaveTargetTableReq,
  querySaveTaskReq,
  queryTestReq,
} from './api'

export default {
  namespace: 'webServiceApiCreate',
  state: {
    formData: {},
  },
  effects: {
    * queryTest({ params, cb }, { call, put }) {
      const res = yield call(queryTestReq, params)
      if (cb) {
        cb(res)
      }
    },
    * queryPreview({ params, cb }, { call, put }) {
      const res = yield call(queryPreviewReq, params)
      if (cb) {
        cb(res)
      }
    },
    * queryGetDatabases({ params, cb }, { call, put }) {
      const res = yield call(queryGetDatabasesReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetTargetTables({ params, cb }, { call, put }) {
      const res = yield call(queryGetTargetTablesReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetCreateTableDDL({ params, cb }, { call, put }) {
      const res = yield call(queryGetCreateTableDDLReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    // 保存每一步骤里的数据
    * saveFormData({ params, cb }, { call, put }) {
      yield put({ type: 'save', payload: params })
      cb && cb()
    },
    * querySaveTargetTable({ params, cb }, { call, put }) {
      const res = yield call(querySaveTargetTableReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetTagetTableFields({ params, cb }, { call, put }) {
      const res = yield call(queryGetTagetTableFieldsReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * querySaveTask({ params, cb }, { call, put }) {
      const res = yield call(querySaveTaskReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetTaskById({ params, cb }, { call, put }) {
      const res = yield call(queryGetTaskByIdReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetWsdlPortOperationMap({ params, cb }, { call, put }) {
      const res = yield call(queryGetWsdlPortOperationMapReq, params)
      if (res && cb) {
        cb(res)
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
}
